package learningApp.Accessories;

import java.util.*;

public class VariableAssignment implements Iterable<Integer>
{
	private Set<Integer> _list;
	private int _startIndex;
	private int _stopIndex;
	
	public VariableAssignment(Set<Integer> list, int length)
	{
		_list = list;
		_startIndex = 1;
		_stopIndex = length;
	}
	
	public VariableAssignment(Set<Integer> list, int startIndex, int stopIndex)
	{
		_list = list;
		_startIndex = startIndex;
		_stopIndex = stopIndex;
	}
	
	public VariableAssignment(VariableAssignment ass1, VariableAssignment ass2)
	{
		_list = new HashSet<Integer>();	//	May also try TreeSet
		_list.addAll(ass1.GetList());
		_list.addAll(ass2.GetList());
	}
	
	public Set<Integer> GetList()
	{
		return _list;
	}
	
	public ChiValue GetValueAtIndex(int index)
	{

		return _list.contains(index) ? ChiValue.PLUS : ChiValue.MINUS;
	}
	
	public boolean IsValid()
	{
		if(_list == null)
		{
			return false;
		}
		for(int i : _list)
		{
			if(i < _startIndex || i > _stopIndex)
			{
				return false;
			}
		}
		return true;
	}

	@Override
	public Iterator<Integer> iterator() {
		return _list.iterator();
	}

	public boolean containsSet(Set<Integer> externalSet) {
		return _list.containsAll(externalSet);
	}
	
	public static VariableAssignment GetRandomAssigment(int startIndex, int stopIndex)
	{
		Set<Integer> includedList = new HashSet<Integer>();
		for(int index = startIndex; index <= stopIndex; index++)
		{
			double randomNumber = Math.random();
			if(randomNumber < 0.5)	//	0<=rand<0.5 : yes, 0.5<=rand<1 : no 
			{
				includedList.add(index);
			}
		}
		return new VariableAssignment(includedList, startIndex, stopIndex);
	}
		
}